home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-19 | 6.9 KB | 268 lines | [TEXT/CWIE] |
- /*
- Problem 05 - Database
-
- type
- StringsHandle = Handle; // Sequence of Pascal Strings packed together
- DatabaseHandle = Handle; // Must be a real handle
-
- procedure DatabaseInit( var database: Handle; field_count: UInt32 );
- procedure DatabaseAddEntry( database: Handle; entry: StringsHandle );
- procedure DatabaseFindEntry( database: Handle; field: UInt32; const match:
- Str255; var entry: StringsHandle );
- procedure DatabaseDeleteEntry( database: Handle; field: UInt32; const match:
- Str255 );
- function DatabaseCount( database: Handle ): UInt32;
- procedure DatabaseGetIndEntry( database: Handle; index: UInt32; var entry:
- StringsHandle );
-
- Your task is to write a set of routines to maintain a database.
-
- DatabaseInit creates a new, empty database ready to accept records with
- field_count string fields. The database is stored in the database Handle.
-
- DatabaseAddEntry adds an entry (which is a Handle to field_count pascal strings
- packed together conceptually numbered 1 to field_count)
-
- DatabaseFindEntry finds an entry whose field (between 1 and field_count) string
- is exactly equal to match. The entry is returned in a newly created handle
- (which will be disposed of using DisposeHandle). Return nil if no match is
- found. If more than one entry matches, you must return the earliest added
- entry.
-
- DatabaseDeleteEntry finds the entry that DatabaseFindEntry would find, and if
- found removes it from the database.
-
- DatabaseCount returns the number of entries in the database.
-
- DatabaseGetIndEntry returns the entries in the Database in the order they were
- entered, 1 for the earliest entered, DatabaseCount the last entered.
-
- All the database information must be stored in the real Mac memory manager
- handle - it will be disposed with DisposeHandle and that must release all
- memory, so do not store any extra information outside the handle. Also, you
- must be able to deal with having multiple databases instantiated
- simultaneously.
-
- You will not be given invalid parameters so you don't need to handle error
- checking, and there will be plenty of memory.
-
- All strings are case sensitive (ie, treated as eight bit binary data).
- */
-
- #include <string.h>
-
-
- #include "Solution.h"
-
- // Fill in your solution and then submit this folder
-
- // Team Name: Team Victory!
-
- UInt32 gFieldCount;
-
-
- OSErr StringFromList( AEDescList *fieldList, StringsHandle *entry );
-
-
-
- pascal void DatabaseInit( DatabaseHandle *database, UInt32 field_count )
- {
- AEDescList dbList;
-
- gFieldCount = field_count;
- (void) AECreateList( nil, 0, false, &dbList );
-
- *database = dbList.dataHandle;
- }//end DatabaseInit
-
- //**************************************************************************************
-
- pascal void DatabaseAddEntry( DatabaseHandle database, StringsHandle entry )
- {
- OSErr anErr = noErr;
-
- AEDescList dbList = { typeAEList, nil };
- dbList.dataHandle = database;
-
- char handleState = HGetState( (Handle)entry );
- HLock( (Handle)entry );
- char *entryPtr = *entry;
-
- AEDescList fieldList;
-
- anErr = AECreateList( nil, 0, false, &fieldList );
- if ( anErr == noErr)
- {
- char *fieldPtr = entryPtr;
- for ( long index = 1; index <= gFieldCount; index++ )
- {
- char *strPtr = &fieldPtr[1]; // get the first character of the field string
- Size strSize = fieldPtr[0]; // the size of the string;
-
- (void) AEPutPtr( &fieldList, index, typeChar, strPtr, strSize );
-
- fieldPtr = strPtr + strSize; // get the next string
- }
- (void) AEPutDesc( &dbList, 0, &fieldList );
-
- (void) AEDisposeDesc( &fieldList );
- }
- HSetState( (Handle)entry, handleState );
- return;
- }//end DatabaseAddEntry
-
- //**************************************************************************************
-
- pascal void DatabaseFindEntry( DatabaseHandle database, UInt32 keyField, ConstStr255Param matchStr, StringsHandle *entryPtr )
- {
- OSErr anErr = noErr;
-
- AEDescList dbList = { typeAEList, nil };
- dbList.dataHandle = database;
-
- long dbCount = 0;
-
- anErr = AECountItems( &dbList, &dbCount );
-
- if ( anErr == noErr )
- {
- for ( long index = 1; index <= dbCount; index++ )
- {
- DescType keyword;
- AEDescList fieldList;
-
- anErr = AEGetNthDesc( &dbList, index, typeAEList, &keyword, &fieldList );
- if ( anErr == noErr )
- {
- DescType keyword;
- DescType actualType;
- Size actualSize;
- char fieldStr[ 256 ];
-
- anErr = AEGetNthPtr( &fieldList, keyField, typeChar, &keyword, &actualType, fieldStr, 255, &actualSize);
- if ( anErr == noErr )
- {
- int cmpResult = strncmp( (char*)&matchStr[1], fieldStr, matchStr[ 0 ] );
- if ( cmpResult == 0 )
- {
- anErr = StringFromList( &fieldList, entryPtr );
- break;
- }
- else
- {
- *entryPtr = nil;
- }
- }
- (void) AEDisposeDesc( &fieldList );
- }
- }
- }
- return;
- }
-
- pascal void DatabaseDeleteEntry( DatabaseHandle database, UInt32 keyField, ConstStr255Param matchStr )
- {
- OSErr anErr = noErr;
-
- AEDescList dbList = { typeAEList, nil };
- dbList.dataHandle = database;
-
- long dbCount;
-
- anErr = AECountItems( &dbList, &dbCount);
-
- if ( anErr == noErr )
- {
- for ( long index = 1; index <= dbCount; index++ )
- {
- DescType keyword;
- AEDescList fieldList;
-
- anErr = AEGetNthDesc( &dbList, index, typeAEList, &keyword, &fieldList );
- if ( anErr == noErr )
- {
- DescType keyword;
- DescType actualType;
- Size actualSize;
- char fieldStr[ 256 ];
-
- anErr = AEGetNthPtr( &fieldList, keyField, typeChar, &keyword, &actualType, fieldStr, 255, &actualSize);
- (void) AEDisposeDesc( &fieldList );
- if ( anErr == noErr )
- {
- int cmpResult = strncmp( (char*)&matchStr[1], fieldStr, actualSize );
- if ( cmpResult == 0 )
- {
- anErr = AEDeleteItem( &dbList, index );
- break;
- }
- }
- }
- }
- }
-
- return;
- }
-
- pascal UInt32 DatabaseCount( DatabaseHandle database )
- {
- OSErr anErr = noErr;
-
- AEDescList dbList = { typeAEList, nil };
- dbList.dataHandle = database;
-
- UInt32 dbCount;
-
- anErr = AECountItems( &dbList, (long*)&dbCount);
-
- return dbCount;
- }
-
- pascal void DatabaseGetIndEntry( DatabaseHandle database, UInt32 index, StringsHandle *entry )
- {
- OSErr anErr = noErr;
-
- AEDescList dbList = { typeAEList, nil };
- dbList.dataHandle = database;
-
- DescType keyword;
- AEDescList fieldList;
-
- anErr = AEGetNthDesc( &dbList, index, typeAEList, &keyword, &fieldList );
- if ( anErr == noErr )
- {
- anErr = StringFromList( &fieldList, entry );
- }
- (void) AEDisposeDesc( &fieldList );
-
- return;
- }//end DatabaseGetIndEntry
-
-
- OSErr StringFromList( AEDescList *fieldList, StringsHandle *entry )
- {
- long fieldCount;
- OSErr anErr = noErr;
-
- *entry = NewHandle( 0 );
-
- anErr = AECountItems( fieldList, &fieldCount);
- if ( anErr == noErr )
- {
- for ( long index = 1; index <= fieldCount; index++ )
- {
- DescType keyword;
- DescType actualType;
- Size actualSize;
- char fieldStr[ 256 ];
-
- anErr = AEGetNthPtr( fieldList, index, typeChar, &keyword, &actualType, &fieldStr[1], 255, &actualSize );
- if ( anErr == noErr )
- {
- fieldStr[0] = actualSize;
- PtrAndHand( fieldStr, *entry, actualSize + 1 );
- }
- }
- }
- return anErr;
- }//end StringFromList